//
//  ACCache.m
//  ACFramework
//
//  Created by caoyq on 2016/12/11.
//  Updatef by caoyq on 2018/1/3.
//

#import "ACCache.h"

@implementation ACCache

#pragma mark - Save

+ (void)saveCustomObject: (id)object forKey: (NSString *)key {
    
    //归档为NSUserDefault支持的NSData类型
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
    //存储得到的NSData
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:key];
    [defaults synchronize];
}

+ (void)saveObject: (id)object forKey: (NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:object forKey:key];
    [defaults synchronize];

}

#pragma mark - Read

+ (id)customObjectWithKey: (NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:key];
    ///将NSData进行反归档
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

+ (id)objectWithKey: (NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:key];
}

#pragma mark - Remove

+ (void)removeObjectWithKey: (NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:key];
    [defaults synchronize];
}

@end
